package com.orhon.smartcampus.modules.schoolaffairs.mutation;

import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.DictionaryDao;
import com.orhon.smartcampus.modules.base.dao.SchoolsDao;
import com.orhon.smartcampus.modules.base.entity.Dictionary;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.schoolaffairs.InputMapper.SchoolNewsInputMapper;
import com.orhon.smartcampus.modules.schoolaffairs.InputMapper.SchoolOpinionInputMapper;
import com.orhon.smartcampus.modules.schoolaffairs.dao.SchoolNewsDao;
import com.orhon.smartcampus.modules.schoolaffairs.dao.SchoolOpinionDao;
import com.orhon.smartcampus.modules.schoolaffairs.entity.SchoolNews;
import com.orhon.smartcampus.modules.schoolaffairs.entity.SchoolOpinion;
import com.orhon.smartcampus.modules.schoolaffairs.gqlInputModel.GQLSchoolNewsInput;
import com.orhon.smartcampus.modules.schoolaffairs.gqlInputModel.GQLSchoolOpinionInput;
import com.orhon.smartcampus.modules.student.dao.EclassDao;
import com.orhon.smartcampus.modules.student.entity.Eclass;
import com.orhon.smartcampus.modules.systemctl.dao.OrgDepartmentsDao;
import com.orhon.smartcampus.modules.systemctl.entity.OrgDepartments;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.modules.user.dao.UsersDao;
import com.orhon.smartcampus.modules.user.entity.Users;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Component
public class AddSchoolOpinionMutation implements DataFetcher {

    @Autowired
    SchoolsDao schoolsDao;

    @Autowired
    TokenService tokenService;

    @Autowired
    SchoolOpinionDao schoolOpinionDao;

    @Autowired
    UsersDao usersDao;

    @Override
    @Transactional(readOnly = false)
    public Object get(DataFetchingEnvironment environment) throws Exception {
        String token = tokenService.getSystemToken();
        Long schoolId = JwtUtils.getSchoolId(token);
        Long userId = JwtUtils.getUserId(token);
        GQLSchoolOpinionInput input = SchoolOpinionInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));
        SchoolOpinion schoolOpinion=null;
        if (input.getId() != null){
            schoolOpinion = schoolOpinionDao.get(SchoolOpinion.class , input.getId());
            schoolOpinion.setUpdatedAt(new Date());
        }
        else{
            schoolOpinion = new SchoolOpinion();
            schoolOpinion.setCreatedAt(new Date());
        }
        if(input.getContent()!=null){
            schoolOpinion.setContent(input.getContent());
        }
        if(input.getUrl()!=null){
            schoolOpinion.setUrl(input.getUrl());
        }
        if (schoolId != null){
            Schools schools = schoolsDao.get(Schools.class ,schoolId);
            schoolOpinion.setSchoolsBySchool(schools);
        }
        if(userId!=null){
            schoolOpinion.setPublisher(usersDao.get(Users.class,userId));
        }
        if(input.getReceive_id()!=null){
            schoolOpinion.setReceiveId(usersDao.get(Users.class,input.getReceive_id()));
        }
        if(input.getExamineState()!=null){
            schoolOpinion.setExamineState(input.getExamineState());
        }
        if(input.getSendingState()!=null){
            schoolOpinion.setSendingState(input.getSendingState());
        }
        try{
            schoolOpinionDao.saveOrUpdate(schoolOpinion);
        }
        catch (Exception e){
            return Result.error(500,""+e);
        }
        return Result.ok();
    }
}
